<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>往来单位</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/layui/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" id="btypename" placeholder="请输入单位名称" />
                </div>


                <a class="layui-btn search_btn" data-type="reload"  id="searchVal">搜索</a>
            </div>
            <shiro:hasPermission name="btype:add">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-normal addBtype_btn" >添加往来单位</a>
                </div>
            </shiro:hasPermission>

            <shiro:hasPermission name="btype:delBatch">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
                </div>
            </shiro:hasPermission>

        </form>
    </blockquote>
    <table id="btypeList" lay-filter="btypeList"></table>
    <!--操作-->
    <script type="text/html" id="btypeListBar">
        <shiro:hasPermission name="btype:del">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="btype:edit">
            <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
        </shiro:hasPermission>

    </script>
</form>
<script type="text/javascript" src="/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate=layui.laydate;

        //用户列表
        var tableIns = table.render({
            elem: '#btypeList',
            url : '/btype/btypes',
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [5,10,15,20,25],
            limit : 5,
            id : "btypeListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'btypename', title: '单位名称', minWidth:100, align:"center"},
                {field: 'btype_typeid', title: '单位类别', minWidth:100, align:"center",templet:function (d) {
                    return d.btype_type.btype_type;
                }},
                {field: 'btype_concat', title: '联系人',width:110, align:'center'},

                {field: 'btype_concattel', title: '联系人电话', minWidth:100, align:'center'},
                {field: 'btypeemail', title: '电子邮箱', minWidth:100, align:'center'},


                {title: '操作', minWidth:175, templet:'#btypeListBar',fixed:"right",align:"center"}
            ]]
        });


        //搜索
        $("#searchVal").click(function () {
            var btypename=$('#btypename').val();

            console.log(btypename);
            tableIns.reload({
                where:{
                    btypename:btypename,
                }
            })
        })
        $(".addBtype_btn").click(function () {
            var addBtype= layui.layer.open({
                title:"添加往来单位",
                type: 2,
                content: '/page/basics/btypeAdd.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function () {
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回往来单位列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });
        })
        //c操作
        table.on('tool(btypeList)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'detail'){ //查看
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    console.log(data)
                    delBtype(data.btypeid);
                    obj.del(); //删除对应行（tr）;的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                editBtype(data);
                //同步更新缓存对应的值
                obj.update({
                    username: '123'
                    ,title: 'xxx'
                });
            } else if(layEvent === 'LAYTABLE_TIPS'){
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });
        //查看
        //修改
        function editBtype(data) {
            layui.layer.open({
                title:"修改往来单位",
                type: 2,
                content: '/page/basics/btypeEdit.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function (layero, index) {
                    //少了这个就不能从父页面向子页面传值
                    var body = layui.layer.getChildFrame('body', index);
                    body.contents().find("#btypeid").val(data.btypeid);
                    body.contents().find("#btypename").val(data.btypename);
                    body.contents().find("#btype_type").val(data.btype_type.btype_typeid);
                    body.contents().find("#btype_concat").val(data.btype_concat);
                    body.contents().find("#btypeemail").val(data.btypeemail);
                    body.contents().find("#btype_concattel").val(data.btype_concattel);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回往来单位列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });
        }


        //删除
        function delBtype(btypeid) {
            $.ajax({
                type:"get",
                url:"/btype/delete",
                data:{btypeid:btypeid},
                success:function (de) {
                    console.log(de);
                    if(de.code==200){
                        layer.msg(de.msg)
                        tableIns.reload();
                    }
                }
            });
        }
        //批量删除
        $('.delAll_btn').click(function () {
            var checkStatus=table.checkStatus("btypeListTable");
            var data=checkStatus.data;
            var idArray=new Array();
            if(data.length>0){
                for(var i=0;i<data.length;i++){
                    idArray.push(data[i].btypeid);
                }
                console.log(idArray);
                $.ajax({
                    url:'/btype/deleteBatch',
                    type:'GET',
                    data:{btypeids:idArray},
                    dataType:"JSON",
                    success:function (res) {
                        if(res.code==200){
                            //提示
                            layui.layer.msg(res.msg);
                            //刷新表格
                            tableIns.reload();
                        }
                    }
                });
            }else{
                layui.layer.msg("你至少要选择一行");
            }
        })
    })

</script>
</body>
</html>